﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PMSWenLvTing.DAL;
using PMSWenLvTing.Model;
using PMSWenLvTing.Model.JiuDian;
using PMSWenLvTing.Utility;

namespace PMSWenLvTing.BLL
{
    public class MainBLL
    {
        MainDAL dal = new MainDAL();


        /// <summary>
        /// 根据条件获取系统baobiao_fabu列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<baobiao_fabu> Get_baobiao_fabu(string strWhere)
        {
            return dal.Get_baobiao_fabu(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统baobiao_zhuangtai列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<baobiao_zhuangtai> Get_baobiao_zhuangtai(string strWhere)
        {
            return dal.Get_baobiao_zhuangtai(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统jiudian列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<jiudian> Get_jiudian(string strWhere)
        {
            return dal.Get_jiudian(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统nianbao列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<nianbao> Get_nianbao(string strWhere)
        {
            return dal.Get_nianbao(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统t_area列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<t_area> Get_t_area(string strWhere)
        {
            return dal.Get_t_area(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统yonghu_jiudian列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<yonghu_jiudian> Get_yonghu_jiudian(string strWhere)
        {
            return dal.Get_yonghu_jiudian(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统yonghu_wenlvting列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<yonghu_wenlvting> Get_yonghu_wenlvting(string strWhere)
        {
            return dal.Get_yonghu_wenlvting(strWhere);
        }

        /// <summary>
        /// 根据条件获取系统yuebao列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        public List<yuebao> Get_yuebao(string strWhere)
        {
            return dal.Get_yuebao(strWhere);
        }

        /// <summary>
        /// 添加一条注册信息到数据库
        /// </summary>
        /// <param name="modeljd"></param>
        /// <param name="modelyh"></param>
        /// <returns></returns>
        public int Add_ZhuCe(jiudian modeljd, yonghu_jiudian modelyh)
        {
            return dal.Add_ZhuCe(modeljd, modelyh);
        }

        /// <summary>
        /// 添加一条注册信息到数据库 子酒店的注册
        /// </summary>
        /// <param name="modeljd"></param>
        /// <returns></returns>
        public int Add_ZhuCeSub(jiudian modeljd)
        {
            return dal.Add_ZhuCeSub(modeljd);
        }

        /// <summary>
        /// 获取酒店短数据
        /// 如果从数据库获取数据，则默认更新内存数据
        /// 如果从内存获取数据，数据不存在则转向从数据库获取数据
        /// </summary>
        /// <param name="lisjdid">酒店编号，没有则获取所有</param>
        /// <param name="fromDB">从数据库获取，否则从内存获取</param>
        /// <returns>返回指定的数据</returns>
        public List<jiudian> Get_jiudian(List<int> lisjdid, bool fromDB)
        {
            if (fromDB == false)
            {
                #region 内存数据
                if (lisjdid == null || lisjdid.Count <= 0)
                {
                    #region 所有数据
                    if (PublicStaticObject.lisJD == null || PublicStaticObject.lisJD.Count <= 0)
                    {
                        //更新内存
                        PublicStaticObject.lisJD = dal.Get_jiudian(null);
                    }
                    return PublicStaticObject.lisJD;
                    #endregion
                }
                else
                {
                    #region 部分数据
                    List<jiudian> lisres = new List<jiudian>();
                    foreach (int jdid in lisjdid)
                    {
                        jiudian model = new jiudian();
                        model = PublicStaticObject.lisJD.Where(kkm => kkm.id == jdid).FirstOrDefault();
                        if (model == null)
                        {
                            break;
                        }
                        else
                        {
                            lisres.Add(model);
                        }
                    }
                    if (lisres.Count == lisjdid.Count)
                    {
                        return lisres;
                    }
                    #endregion
                }
                #endregion
            }

            #region 从数据库获取

            #region 获取数据
            string strWhere = "";
            if (lisjdid == null || lisjdid.Count <= 0)
            {
                //所有数据
                strWhere = "1=1";
            }
            else
            {
                //部分数据
                strWhere = string.Format("id in ({0})", string.Join(",", lisjdid));
            }

            List<jiudian> lisdb = dal.Get_jiudian(strWhere);
            if (lisdb == null)
            {
                lisdb = new List<jiudian>();
            }
            #endregion

            #region 更新数据
            foreach (jiudian jddb in lisdb)
            {
                if (PublicStaticObject.lisJD.Exists(kkm => kkm.id == jddb.id))
                {
                    //替换
                    PublicStaticObject.lisJD.RemoveAll(kkm => kkm.id == jddb.id);
                    PublicStaticObject.lisJD.Add(jddb);
                }
                else
                {
                    //追加
                    PublicStaticObject.lisJD.Add(jddb);
                }
            }
            #endregion

            #endregion

            return lisdb;
        }

        /// <summary>
        /// 获取酒店数据【所有下级】
        /// 如果从数据库获取数据，则默认更新内存数据
        /// 如果从内存获取数据，数据不存在则转向从数据库获取数据
        /// 默认获取的是此区域和此区域的所有下级的酒店数据
        /// </summary>
        /// <param name="lisaid">区域编号，没有则获取所有</param>
        /// <param name="fromDB">从数据库获取，否则从内存获取</param>
        /// <returns>返回指定的数据</returns>
        public List<jiudian> Get_jiudian_aid(List<int> lisaid, bool fromDB)
        {
            //转换aid，获取aid及所有下级的aid
            lisaid = Self_GetAIDSub_HaiNan(lisaid);

            if (fromDB == false)
            {
                #region 内存数据
                if (lisaid == null || lisaid.Count <= 0)
                {
                    #region 所有数据
                    if (PublicStaticObject.lisJD == null || PublicStaticObject.lisJD.Count <= 0)
                    {
                        //更新内存
                        PublicStaticObject.lisJD = dal.Get_jiudian(null);
                    }
                    return PublicStaticObject.lisJD;
                    #endregion
                }
                else
                {
                    #region 部分数据
                    List<jiudian> lisres = new List<jiudian>();
                    foreach (int aid in lisaid)
                    {
                        jiudian model = new jiudian();
                        model = PublicStaticObject.lisJD.Where(kkm => kkm.aid == aid).FirstOrDefault();
                        if (model == null)
                        {
                            break;
                        }
                        else
                        {
                            lisres.Add(model);
                        }
                    }
                    if (lisres.Count == lisaid.Count)
                    {
                        return lisres;
                    }
                    #endregion
                }
                #endregion
            }

            #region 从数据库获取

            #region 获取数据
            string strWhere = "";
            if (lisaid == null || lisaid.Count <= 0)
            {
                //所有数据
                strWhere = "1=1";
            }
            else
            {
                //部分数据
                strWhere = string.Format("aid in ({0})", string.Join(",", lisaid));
            }

            List<jiudian> lisdb = dal.Get_jiudian(strWhere);
            if (lisdb == null)
            {
                lisdb = new List<jiudian>();
            }
            #endregion

            #region 更新数据
            foreach (jiudian jddb in lisdb)
            {
                if (PublicStaticObject.lisJD.Exists(kkm => kkm.id == jddb.id))
                {
                    //替换
                    PublicStaticObject.lisJD.RemoveAll(kkm => kkm.id == jddb.id);
                    PublicStaticObject.lisJD.Add(jddb);
                }
                else
                {
                    //追加
                    PublicStaticObject.lisJD.Add(jddb);
                }
            }
            #endregion

            #endregion

            return lisdb;
        }

        /// <summary>
        /// 根据条件获取baobiao_zhuangtai的条数
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int Get_BBZhuangTaiCount(string strWhere)
        {
            return dal.Get_BBZhuangTaiCount(strWhere);
        }

        /// <summary>
        /// 新增一条年报数据
        /// </summary>
        /// <param name="model">年报数据</param>
        /// <param name="modelbbzt">报表状态数据</param>
        /// <returns></returns>
        public int Add_nianbao(nianbao model, baobiao_zhuangtai modelbbzt)
        {
            return dal.Add_nianbao(model, modelbbzt);
        }

        /// <summary>
        /// 新增一条月报数据
        /// </summary>
        /// <param name="model">月报数据</param>
        /// <param name="modelbbzt">报表状态数据</param>
        /// <returns></returns>
        public int Add_yuebao(yuebao model, baobiao_zhuangtai modelbbzt)
        {
            return dal.Add_yuebao(model, modelbbzt);
        }

        /// <summary>
        /// 修改用户酒店账号状态
        /// </summary>
        /// <param name="yhjdid">用户酒店id</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public int Modify_yonghu_jiudian_zt(int yhjdid, int zt)
        {
            return dal.Modify_yonghu_jiudian_zt(yhjdid, zt);
        }

        /// <summary>
        /// 添加一条酒店用户数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add_yonghu_jiudian(yonghu_jiudian model)
        {
            return dal.Add_yonghu_jiudian(model);
        }

        /// <summary>
        /// 修改用户酒店账号基本信息
        /// 姓名 账号 酒店权限
        /// </summary>
        /// <param name="yhjdid">用户酒店id</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public int Modify_yonghu_jiudian_info(yonghu_jiudian model)
        {
            return dal.Modify_yonghu_jiudian_info(model);
        }

        /// <summary>
        /// 根据酒店编号获取酒店的报表统计数据 
        /// 不包含酒店名称
        /// </summary>
        /// <param name="lisjdid">酒店编号</param>
        /// <returns></returns>
        public List<DTO_JDTongJi> Get_DTO_JDTongJi(List<int> lisjdid)
        {
            return dal.Get_DTO_JDTongJi(lisjdid);
        }

        /// <summary>
        /// 根据条件获取jiudian的条数
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int Get_JiuDianCount(string strWhere)
        {
            return dal.Get_JiuDianCount(strWhere);
        }

        /// <summary>
        /// 获取海南相关的area数据，1-4级数据都获取
        /// 默认放到内存，当内存中不存在则从数据库中获取
        /// </summary>
        /// <returns></returns>
        public List<t_area> Get_t_area_hainan()
        {
            if (PublicStaticObject.lisArea == null || PublicStaticObject.lisArea.Count <= 0)
            {
                List<t_area> lisAll = new List<t_area>();
                List<t_area> lisLv1 = new List<t_area>();
                List<t_area> lisLv2 = new List<t_area>();
                List<t_area> lisLv3 = new List<t_area>();

                lisLv1 = dal.Get_t_area(string.Format("areaId=2205"));
                lisLv2 = dal.Get_t_area(string.Format("parentId=2205"));
                lisLv3 = dal.Get_t_area(string.Format("parentId in ({0})",string.Join(",",lisLv2.Select(kkm=>kkm.areaId))));

                lisAll.AddRange(lisLv1);
                lisAll.AddRange(lisLv2);
                lisAll.AddRange(lisLv3);

                PublicStaticObject.lisArea = lisAll;

                return PublicStaticObject.lisArea.OrderBy(kkm=>kkm.areaCode).ToList();

            }
            else
            {
                return PublicStaticObject.lisArea.OrderBy(kkm => kkm.areaCode).ToList();
            }
        }

        /// <summary>
        /// 修改报表的状态
        /// </summary>
        /// <param name="bblx">类型 1月报 2年报</param>
        /// <param name="bbid">报表ID，不同类型的报表的ID</param>
        /// <param name="jdid">酒店id</param>
        /// <param name="zt">审核状态 0未审核 1审核通过 2审核拒绝</param>
        /// <param name="beizhu">备注，被拒绝时候的说明</param>
        /// <param name="yywltid">文旅厅用户id，审核人</param>
        /// <returns></returns>
        public int ModifyBBZhuangTai(int bblx, long bbid, int jdid, int zt, string beizhu, int yywltid)
        {
            return dal.ModifyBBZhuangTai(bblx, bbid, jdid, zt, beizhu, yywltid);
        }

        /// <summary>
        /// 修改用户文旅厅账号状态
        /// </summary>
        /// <param name="yhwltid">用户文旅厅id</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public int Modify_yonghu_wenlvting_zt(int yhwltid, int zt)
        {
            return dal.Modify_yonghu_wenlvting_zt(yhwltid, zt);
        }

        /// <summary>
        /// 添加一条文旅厅用户数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add_yonghu_wenlvting(yonghu_wenlvting model)
        {
            return dal.Add_yonghu_wenlvting(model);
        }

        /// <summary>
        /// 修改文旅厅用户信息 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Modify_yonghu_wenlvting_info(yonghu_wenlvting model)
        {
            return dal.Modify_yonghu_wenlvting_info(model);
        }

        /// <summary>
        /// 根据条件获取baobiao_fabu的条数
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int Get_BBFaBuCount(string strWhere)
        {
            return dal.Get_BBFaBuCount(strWhere);
        }

        /// <summary>
        /// 添加一条报表发布数据
        /// </summary>
        /// <param name="modelfb">报表发布</param>
        /// <param name="lisjdid">酒店id</param>
        /// <returns></returns>
        public int Add_baobiao_fabu(baobiao_fabu modelfb, List<int> lisjdid)
        {
            return dal.Add_baobiao_fabu(modelfb, lisjdid);
        }

            #region 自定义方法区
            /// <summary>
            /// 获取aid对应的aid和下级的aid
            /// </summary>
            /// <param name="lisaid">要获取的aid</param>
            /// <returns></returns>
            public List<int> Self_GetAIDSub_HaiNan(List<int> lisaid)
        {
            List<int> lisRes = new List<int>();
            List<t_area> lisarea_lv2 = new List<t_area>();
            List<t_area> lisarea_lv3 = new List<t_area>();
            List<t_area> lisarea_lv4 = new List<t_area>();
            List<t_area> lisarea = Get_t_area_hainan();
            if (lisarea == null || lisarea.Count <= 0)
            {
                return lisaid;
            }
            foreach (int aid in lisaid)
            {
                lisRes.Add(aid);
                lisarea_lv2 = lisarea.Where(kkm => kkm.parentId == aid).ToList();
                if (lisarea_lv2 != null && lisarea_lv2.Count > 0)
                {
                    lisRes.AddRange(lisarea_lv2.Select(kkm => kkm.areaId));
                    foreach (t_area lv2 in lisarea_lv2)
                    {
                        lisarea_lv3 = lisarea.Where(kkm => kkm.parentId == lv2.areaId).ToList();
                        if (lisarea_lv3 != null && lisarea_lv3.Count > 0)
                        {
                            lisRes.AddRange(lisarea_lv3.Select(kkm => kkm.areaId));
                            foreach (t_area lv3 in lisarea_lv3)
                            {
                                lisarea_lv4 = lisarea.Where(kkm => kkm.parentId == lv3.areaId).ToList();
                                if (lisarea_lv4 != null && lisarea_lv4.Count > 0)
                                {
                                    lisRes.AddRange(lisarea_lv4.Select(kkm => kkm.areaId));
                                }
                            }
                        }
                    }
                }
            }
            return lisRes;
        }

        #endregion


    }
}